//
//  TFVideoArea1to1.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/11.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFVideoArea1to1: UIView, UITableViewDelegate, UITableViewDataSource ,TFVideoAreaCellDelegate{
 
    @objc public weak var delegate : TFVideoArea1to1Delegate?
    @objc var vc:TFVideoAreaCell?
    //全屏
    @objc var isFull:Bool = false
    @objc let tableView = UITableView()
    @objc let videoSessionsArray = NSMutableArray()
    //显示奖杯
    @objc var showCup:Bool = false

    func getRtcModel()->TalkfunRtcModel
    {
        let model = TalkfunRtcModel()

        return model
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    
        self.tableView.isScrollEnabled = false
     
        //不要下划线
        self.tableView.separatorStyle = .none
         self.tableView.backgroundColor = UIColor(red: 19/255.0, green: 21/255.0, blue: 25/255.0, alpha: 1)
        self.tableView.delegate   = self
        self.tableView.dataSource = self
        self.addSubview(self.tableView)
    
        self.tableView.register(TFVideoAreaCell.self, forCellReuseIdentifier: "TFVideoAreaCell")
     
        //数据源

         //TODO:老师
          let teacher   = self.getRtcModel()
           teacher.role  = .roleSpadmin
           teacher.video = .openForZHUBO
           teacher.audio = .openForZHUBO
           teacher.nickname = ""
           self.videoSessionsArray.add(teacher)
           //TODO:学员
           let student   = self.getRtcModel()
           student.role = .roleeUser
           student.video = .openForUser
           student.audio = .openForUser
           student.nickname = ""
        self.videoSessionsArray.add(student)
       
        self.tableView.reloadData()
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.tableView.frame = self.bounds
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //双击了
    func objectDidDragged(_ model: TFVideoAreaCell) {
        if self.delegate != nil && (self.delegate?.responds(to: #selector(TFVideoArea1to1Delegate.objectDidDraggedCourse(_:))))!{
            self.delegate?.objectDidDraggedCourse(model)
           
        }
    }
    
    func dealSingleTap(_ model: TFVideoAreaCell) {
        NotificationCenter.default.post(name: Notification.Name(rawValue: "keyboardWillHide"), object: nil)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.videoSessionsArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 1.创建cell,此时cell是可选类型
        
       let cellView = tableView.dequeueReusableCell(withIdentifier: "TFVideoAreaCell") as! TFVideoAreaCell

        let model =  self.videoSessionsArray[indexPath.row] as! TalkfunRtcModel
    
        cellView.backgroundColor = self.tableView.backgroundColor

        //取消选中状态
        cellView.selectionStyle = .none

       cellView.showCup = self.showCup
       cellView.model = model;
       cellView.videoView.frame = cellView.bounds
       cellView.delegate = self
       for i in 0..<cellView.videoView.subviews.count {
        let view =  cellView.videoView.subviews[i] as UIView

        view.frame = cellView.videoView.bounds

        }
        return cellView;
    }
    // 设置cell高度
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return self.frame.size.width*0.75
        }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.endEditing(true)
    }
    // 选中cell后执行此方法
       func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
           
           tableView.deselectRow(at: indexPath, animated: true)
           
       }
    
    // MARK: 下线
    @objc func kick()
    {
        self.tableView.reloadData()
    }
    // MARK: 直播结束
    @objc public func liveStop(_ userNickname:String,zhubonickname:String)
    {
        self.videoSessionsArray.removeAllObjects()
        if self.vc != nil {
            self.vc!.zoomButton.isHidden = true
        }
       
        //TODO:老师
        let teacher   = self.getRtcModel()
        teacher.role  = .roleSpadmin
        teacher.video = .openForZHUBO
        teacher.audio = .openForZHUBO;

        teacher.nickname = zhubonickname
        self.videoSessionsArray.add(teacher)
   
        //TODO:学员
        let  student   = self.getRtcModel()
        student.role = .roleeUser
        student.video = .openForUser
        student.audio = .openForUser
        student.nickname = userNickname
     
        self.videoSessionsArray.add(student)
        self.tableView.reloadData()

    }
    @objc func exist(_ model:TalkfunRtcModel)->Bool

    {
            //是否已经存在
        var Exist:Bool = false
        for i in 0..<self.videoSessionsArray.count {
           let obj = self.videoSessionsArray[i] as! TalkfunRtcModel
        
        if (obj.xid == model.xid) {
            Exist = true

        }
         
       
         }
         return Exist
    }
    @objc func addModel(_ model:TalkfunRtcModel)
    { //是否已经全屏
               if(self.isFull){
                   if (self.vc != nil) {
                       self.isFull = false
                       self.vc!.frame = self.vc!.tempFrame;
                       self.tableView.addSubview(self.vc!)
                       self.tableView.reloadData()
                   }
               }
        
        //初始化数据
        for i in 0..<self.videoSessionsArray.count {

                   let obj = self.videoSessionsArray[i] as! TalkfunRtcModel

                   if (obj.role == model.role) {
                       self.videoSessionsArray.remove(obj)
                      
                       break;
                       
                   }
               }
        
      
        if (model.role == .roleSpadmin) {
            self.videoSessionsArray.insert(model, at: 0)
                
             }else{
                 self.videoSessionsArray.add(model)
              
             }

        self.tableView.reloadData()
      
    }
    @objc func updateModel(_ model:TalkfunRtcModel)
    {
          //是否已经全屏
        if(self.isFull){
            if (self.vc != nil) {
                self.isFull = false
                self.vc!.frame = self.vc!.tempFrame;
                self.tableView.addSubview(self.vc!)
                self.tableView.reloadData()
            }
        }

             //初始化数据
        for i in 0..<self.videoSessionsArray.count {
                 let obj = self.videoSessionsArray[i] as! TalkfunRtcModel

                 if (obj.xid == model.xid) {
                     obj.score = model.score
                     obj.audio = model.audio
                     obj.drawPower = model.drawPower
                     obj.video = model.video
                     
                 }
             }
        if self.vc != nil {
            if (self.vc!.zoomButton.isHidden == false) {
                self.vc!.zoomButton.isHidden = true
            }
        }
        self.tableView.reloadData()
          
    }
    @objc func removeFromVideo(_ model:TalkfunRtcModel)

    {
        if(self.isFull){
            if (self.vc != nil) {
                self.isFull = false
                self.vc!.frame = self.vc!.tempFrame;
                self.tableView.addSubview(self.vc!)
                self.tableView.reloadData()
            }
        }
        for i in 0..<self.videoSessionsArray.count {
            let model = self.videoSessionsArray[i] as! TalkfunRtcModel
            if (model.xid == model.xid) {
                self.videoSessionsArray.remove(model)
               
                break;
                
            }
        }
        self.tableView.reloadData()
       
    }
    @objc func rtcDown(_ model:TalkfunRtcModel)

    {
        if(self.isFull){
            if (self.vc != nil) {
                self.isFull = false
                self.vc!.frame = self.vc!.tempFrame;
                self.tableView.addSubview(self.vc!)
                self.tableView.reloadData()
            }
        }
        
        for i in 0..<self.videoSessionsArray.count {
            let model = self.videoSessionsArray[i] as! TalkfunRtcModel
            if (model.role == .roleeUser) {
                if (model.xid == model.xid) {
                    let student = self.getRtcModel()
                    student.role = .roleeUser
                    student.video = .openForUser
                    student.audio = .openForUser
                    self.videoSessionsArray.replaceObject(at: i, with: student)
                   
                    break;
                    
                }
            }
            
        }
        self.tableView.reloadData()
       
    }
    
    
}
@objc protocol TFVideoArea1to1Delegate : NSObjectProtocol {
    //双击了
    func objectDidDraggedCourse(_ cell : TFVideoAreaCell)
    //单击
    func dealSingleTapCourse(_ cell : TFVideoAreaCell)
   
}
